import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
//import QtQuick.Controls.Styles

import Shared

/*基础上GamePage.qml，所以有errorMessage,infoMessage。
  有1个title，1个ListView和2个GameButton。
*/
GamePage {
    /*QQ会重载吗？
    */
    function init()
    {
        console.log("show ParaLaji.qml")
        row_info.vis= false;
        deviceHandler.updateParaUI();
    }


    Text {
        id: txt_title
        anchors.top: parent.top


        text: qsTr("垃圾车时序参数")
        color: "green"
        anchors.horizontalCenter: parent.horizontalCenter
        font.pointSize: 48
    }


    //上升－下降间隔最小值
    RowLayout{
        id: row1
        anchors.top: txt_title.bottom

        Text {
            text: qsTr("升降间隔小值")
            color: "green"
        }

        ComboBox{
            id: combobox_gap_min
            width:parent.width/5
            model: ["1s", "2s", "4s"]

            currentIndex: deviceHandler.laji_gap_min_idx;
        }

        Button{
            id: btn_gap_min_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read Laji gap_min");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_laji_gap_min_idx, 0);
            }
        }

        Button{
            id: btn_gap_min_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write Laji gap_min");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_laji_gap_min_idx, combobox_gap_min.currentIndex);
            }
        }
    }



    //上升-下降间隔最大值
    RowLayout{
        id: row2
        anchors.top: row1.bottom

        Text {
            text: qsTr("升降间隔大值")
            color: "green"
        }

//        TextField{

//            text: "0"
//        }
        ComboBox{
            id: combobox_gap_max
            width:btn_gap_min_rd.width

            model: ["8s", "16s", "40s", "60s", "90s"]

            currentIndex: deviceHandler.laji_gap_max_idx;
        }

        Button{
            id: btn_gap_max_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read Laji interval_max");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_laji_gap_max_idx, 0);
            }
        }

        Button{
            id: btn_gap_max_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write Laji interval_max");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_laji_gap_max_idx, combobox_gap_max.currentIndex);
            }
        }
    }


    //称重值保持显示时长
    RowLayout{
        id: row3
        anchors.top: row2.bottom

        Text {
            text: qsTr("称重保持时长")
            color: "green"
        }

        ComboBox{
            id: combobox_wt_hold_tmo
            width:btn_gap_min_rd.width

            model: ["1s", "2s", "3s", "4s"]

            currentIndex: deviceHandler.laji_hold_tmo_idx;
        }

//        TextField{

//            text: "0"
//        }

        Button{
            id: btn_wt_wt_hold_tmo_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read Laji wt hold time");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_laji_wt_hold_tmo_idx, 0);
            }
        }

        Button{
            id: btn_wt_wt_hold_tmo_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write Laji wt hold time");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_laji_wt_hold_tmo_idx, combobox_wt_hold_tmo.currentIndex);
            }
        }
    }


    //gps上报间隔。
    RowLayout{
        id: row_gps_gap
        anchors.top: row3.bottom

        Text {
            text: qsTr("定位上报间隔")
            color: "green"
        }

        TextField{
            id: txtfd_gps_gap

            text: deviceHandler.laji_gps_gap
            //text: deviceHandler.plc_addr.toString()   //测试ok
            color: "white"
        }


        Button{
            id: btn_gps_gap_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read Laji wt gps gap");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_laji_gps_gap, 0);
            }
        }

        Button{
            id: btn_gps_gap_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write Laji gps gap");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_laji_gps_gap, Number(txtfd_gps_gap.text));
            }
        }
    }


    //零区值设置数值。
    RowLayout{
        id: row_zzone_val
        anchors.top: row_gps_gap.bottom

        Text {
            text: qsTr("零区设置数值")
            color: "green"
        }

        TextField{
            id: txtfd_zzone_val

            text: deviceHandler.laji_zzone_val
            //text: deviceHandler.plc_addr.toString()   //测试ok
            color: "white"
        }


        Button{
            id: btn_zzone_val_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read Laji zzone val");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_laji_zzone_val, 0);
            }
        }

        Button{
            id: btn_zzone_val_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write Laji zzone val");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_laji_zzone_val, Number(txtfd_zzone_val.text));
            }
        }
    }


    //零区值保持时长。
    RowLayout{
        id: row_zzone_tmo
        anchors.top: row_zzone_val.bottom

        Text {
            text: qsTr("零区保持时长")
            color: "green"
        }

        TextField{
            id: txtfd_zzone_tmo

            text: deviceHandler.laji_zzone_tmo
            //text: deviceHandler.plc_addr.toString()   //测试ok
            color: "white"
        }


        Button{
            id: btn_zzone_tmo_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read Laji zzone tmo");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_laji_zzone_tmo, 0);
            }
        }

        Button{
            id: btn_zzone_tmo_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write Laji zzone tmo");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_laji_zzone_tmo, Number(txtfd_zzone_tmo.text));
            }
        }
    }


    //DTU服务器地址和端口。
    RowLayout{
        id: row_ip
        anchors.top: row_zzone_tmo.bottom
        width: parent.width

        Row{
            anchors.fill: parent

            Text {
                text: qsTr("DTU地址")
                color: "green"
            }

            TextField{
                id: txtfd_ip1

                text: qsTr("0")
                //text: deviceHandler.plc_addr.toString()   //测试ok
                color: "white"

                inputMask: "999"
                width: parent.width/5
            }
            TextField{
                id: txtfd_ip2
                width: parent.width/5

                text: qsTr("0")
                //text: deviceHandler.plc_addr.toString()   //测试ok
                color: "white"

                inputMask: "999"

            }

            TextField{
                id: txtfd_ip3
                width: parent.width/5

                text: qsTr("0")
                //text: deviceHandler.plc_addr.toString()   //测试ok
                color: "white"

                inputMask: "999"
            }

            TextField{
                id: txtfd_ip4
                width: parent.width/5

                text: qsTr("0")
                //text: deviceHandler.plc_addr.toString()   //测试ok
                color: "white"

                inputMask: "999"
            }
        }
    }
    RowLayout{
        id: row_port
        anchors.top: row_ip.bottom

        Text {
            text: qsTr("DTU端口")
            color: "green"
        }

        TextField{
            id: txtfd_port

            text: qsTr("0")
            //text: deviceHandler.plc_addr.toString()   //测试ok
            color: "white"
            inputMask: "00000"  //6位数字，最大65535。
        }

        Button{
            id: btn_ip_port_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write DTU SOCKA's IP&PORT");
                row_info.vis= true;
                deviceHandler.data10h_to_periph2(GameSettings.addr_laji_dtu_ip_port, Number(txtfd_ip1.text),Number(txtfd_ip2.text),Number(txtfd_ip3.text),Number(txtfd_ip4.text),Number(txtfd_port.text));
            }
        }
    }

    RowLayout{
        id: row_curve
        anchors.top: row_port.bottom
        anchors.topMargin: 10

        Text {
            text: qsTr("实时曲线记录")
            color: "green"
        }

        ComboBox{
            id: combobox_curve

            model: ["disable", "enable"]

            currentIndex: deviceHandler.laji_curve;
        }

        Button{
            id: btn_curve_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read LAJI curve");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_laji_curve, 0);
            }
        }

        Button{
            id: btn_curve_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write LAJI curve");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_laji_curve, combobox_curve.currentIndex);
            }
        }
    }


    //提示信息。
    RowLayout{
        property bool vis: true

        id: row_info

        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        anchors.horizontalCenter: parent.horizontalCenter
        //Layout.alignment: Qt.AlignHCenter

        Text{
            text:qsTr(" ")      //占位，以免下面的Text{}隐藏时，造成下面的控件位置变化。
        }

        Text{
            id: txt_info_laji
            anchors.centerIn: parent.Center
            visible: deviceHandler.txt_ok_v && parent.vis


            //text: qsTr("成功")
            text: deviceHandler.txt_info
            color: deviceHandler.txt_color  //"green"
        }
    }


    Timer{
        id: timer_info_laji
        interval: 500
        running: deviceHandler.txt_ok_v && row_info.vis

        onTriggered: {
            console.log("timer tmo")
            row_info.vis= false
            //row_info.txt_info_set.visible= false;   //和绑定属性deviceHandler.txt_ok_v不同步了。
        }
    }

//    //帮助信息
//    Text {
//        //id: txt_title
//        anchors.bottom: parent.bottom
//        anchors.bottomMargin: 10
//        anchors.horizontalCenter: parent.horizontalCenter

//        text: qsTr("须先依次点击读取，以更新界面")
//        color: "green"


//        font.pointSize: 24
//    }
}
